package main

import (
	"bufio"
	"fmt"
	"io"
	"os"
	"strconv"
	"strings"
)

func convertToBin(n int) string {
	result := ""
	if n == 0 {
		result = "0"
	} else {
		for ; n > 0; n /= 2 {
			lsp := n % 2
			result = strconv.Itoa(lsp) + result
		}
	}

	return result
}

func printFile(filename string) {
	file, err := os.Open(filename)
	if err != nil {
		panic(err)
	}

	printFileContents(file)

}

// 使用接口
func printFileContents(reader io.Reader) {
	scanner := bufio.NewScanner(reader)

	//这类似其他语言的while
	for scanner.Scan() {
		fmt.Println(scanner.Text())
	}
}

func forever() {
	//while True
	for {
		fmt.Println("abcdefghijkl")
	}
}

func main() {
	fmt.Println(
		convertToBin(5),
		convertToBin(13),
		convertToBin(45344534),
		convertToBin(0),
	)

	printFile("chapter02-basic/loop/abc.txt")

	s := `afdsf"d"
     	kkkk
		124

     p`
	printFileContents(strings.NewReader(s))

	//这是死循环，用ctrl+c停止
	//forever()
}
